<template>
  <div class="app-container">
    <el-button round style="font-size: 12px" @click="back"><i class="el-icon-back"></i>返回</el-button>
    <br><br>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
    <el-form-item label="楼层名称">
      <el-input
        v-model="queryParams.floorName"
        placeholder="请输入楼层名称"
        clearable
        size="small"
        style="width: 240px"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
      <el-button type="primary" size="mini" @click="onAdd">新增</el-button>
    </el-form-item>
    </el-form>

    <span class="spanClass">
      <el-card class="box-card" v-for="l in floors">
      <div slot="header" class="clearfix">
        <span><el-link @click="to(l.id)">{{ l.floorName }}</el-link></span>
        <i class="el-icon-tickets" style="margin-left: 190px"></i>
      </div>
      楼层编号：{{l.floorId}} <br><br>
      楼层总房间数：{{l.roomNum}}<br><br>
        <!--      <div>楼栋总房间数：{{l.loudongNum}}</div>-->
      <el-button icon="el-icon-edit" size="mini" style="float: left" @click="onUpdate(l)"></el-button>
      <el-button icon="el-icon-delete" size="mini" style="float: right" @click="del(l.id)"></el-button>
    </el-card>
    </span>

    <el-dialog
      title="新增/修改楼层"
      :visible.sync="dialogVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="楼层名称" prop="floorName" label-width="100px">
          <el-input v-model="ruleForm.floorName"></el-input>
        </el-form-item>
        <el-form-item label="楼层编号" prop="floorId" label-width="100px">
          <el-input v-model="ruleForm.floorId"></el-input>
        </el-form-item>
        <el-form-item label="总房间数" prop="roomNum" label-width="100px" v-if="!ruleForm.roomNum">
          <el-input v-model="ruleForm.roomNum"></el-input>
        </el-form-item>
        <el-form-item label="总房间数" prop="roomNum" label-width="100px" v-if="ruleForm.roomNum">
          <el-input v-model="ruleForm.roomNum" disabled></el-input>
        </el-form-item>
        <el-form-item label-width="100px">
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="dialogVisible=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {getList,saveOrUpdate,del} from "@/api/site/floor";

export default {
  name: "floor",
  data(){
    var validateNum = (rule, value, callback) => {
      if (value === '0') {
        callback(new Error('请输入正确的房间数'));
      }
      callback();
    }
    return{
      dialogVisible:false,
      rules: {
        floorName: [
          {required: true, message: '请输入楼层名称', trigger: 'blur'},
        ],
        floorId: [
          {required: true, message: '请输入楼层编号', trigger: 'blur'},
        ],
        roomNum: [
          { validator: validateNum, trigger: 'blur' }
        ]
      },
      ruleForm:{},
      queryParams:{
        floorName:'',
        loudongId:''
      },
      floors:''
    }
  },
  created() {
    this.queryParams.loudongId=this.$route.query.loudongId
    this.getList()
  },
  methods:{
    to(id){
      this.$router.push({path:'/garden/site/room',query:{floorId:id}})
    },
    onUpdate(row){
      this.ruleForm=row
      this.dialogVisible=true
    },
    onAdd(){
      this.dialogVisible=true
      this.ruleForm={
        floorName:'',
        floorId:"",
        roomNum:null,
        loudongId:null
      }
    },
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.loudongId=this.queryParams.loudongId
          saveOrUpdate(this.ruleForm).then(res=>{
            this.$message.success('操作成功')
            this.dialogVisible=false
            this.getList()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    back(){
      this.$router.back()
    },
    getList(){
      getList(this.queryParams).then(res=>{
        this.floors=res.data
      })
    },
    del(id){
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style scoped>
.spanClass div{
  display: inline-block;
}
.box-card{
  width: 300px;
  font-size: 14px;
  padding: 6px;
  margin: 5px;
}
</style>
